package dynamic.util;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import dynamic.metadata.EntityClass;
import dynamic.metadata.EntityClassImpl;
import dynamic.metadata.EntityProperty;
import dynamic.metadata.property.TableId;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TableInfoReader {

    private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://127.0.0.1:3306/tt?serverTimezone=Asia/Shanghai", "root", "root")
            .schema("tt")
            .build();

    /**
     * 策略配置
     */
    protected static StrategyConfig.Builder strategyConfig() {
        return new StrategyConfig.Builder();
    }

    /**
     * 全局配置
     */
    protected static GlobalConfig.Builder globalConfig() {
        return new GlobalConfig.Builder();
    }

    /**
     * 包配置
     */
    protected static PackageConfig.Builder packageConfig() {
        return new PackageConfig.Builder();
    }

    /**
     * 注入配置
     */
    protected static InjectionConfig.Builder injectionConfig() {
        // 测试自定义输出文件之前注入操作，该操作再执行生成代码前 debug 查看
        return new InjectionConfig.Builder().beforeOutputFile((tableInfo, objectMap) -> {
            System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
        });
    }

    public static List<TableInfo> getTableInfoList() {
        ConfigBuilder configBuilder = new ConfigBuilder(null, DATA_SOURCE_CONFIG, strategyConfig().build(), null, globalConfig().build(), null);
        return configBuilder.getTableInfoList();
    }

    public static Map<String, EntityClass> entityClassMap(String packageName) {
        ConfigBuilder configBuilder = new ConfigBuilder(null, DATA_SOURCE_CONFIG, strategyConfig().build(), null, globalConfig().build(), null);
        List<TableInfo> list = configBuilder.getTableInfoList();
        HashMap<String, EntityClass> hashMap = new HashMap();
        for (TableInfo tableInfo : list) {
            EntityClassImpl entityClass = new EntityClassImpl(packageName, tableInfo.getEntityName());
            Map<String, EntityProperty> propertyMap = new HashMap();
            List<TableField> fields = tableInfo.getFields();
            for (TableField field : fields) {
                EntityProperty entityProperty = new EntityProperty(entityClass);
                entityProperty.setPropertyName(field.getPropertyName());
                Class<?> clazz = TypeTranslate.getClassByType(field.getColumnType());
                entityProperty.setPropertyClass(clazz);
                entityProperty.setFieldName(field.getName());
                if (field.isKeyFlag()) {
                    TableId tableId = new TableId(field.getAnnotationColumnName());
                    if (field.isKeyIdentityFlag()) {
                        tableId.setType(IdType.AUTO);
                    }
                    entityProperty.setTableId(tableId);
                }
                entityProperty.setEntityClass(entityClass);
                propertyMap.put(field.getPropertyName(), entityProperty);
            }
            entityClass.setProperties(propertyMap);
            hashMap.put(entityClass.getName(), entityClass);
        }
        return hashMap;
    }
}
